package zero.tech.games.gate.hot;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import zero.tech.games.common.RedisConsts;
import zero.tech.games.entity.games.UserRole;
import zero.tech.games.gate.mapper.UserRoleMapper;
import zero.tech.redis.cache.RedisUtil;
@Component
public class UserUtil {
	@Autowired
	private UserRoleMapper userRoleMapper;
	@Autowired
	private RedisUtil redisUtil;
	public UserRole getUserRole(int uid) {
		UserRole userRole = null;
		String str=(String) redisUtil.hget(RedisConsts.A_USERROLE, String.valueOf(uid));
		if(StringUtils.isBlank(str)) {
			userRole=userRoleMapper.getUserRole(uid);
			if(userRole!=null) {
				redisUtil.hset(RedisConsts.A_USERROLE, String.valueOf(uid), JSONObject.toJSONString(userRole));
			}
		}else {
			userRole=JSONObject.parseObject(str, UserRole.class);
		}
		return userRole;
	}
	public void insertUserRole(UserRole userRole) {
		int uid=userRole.getUid();
		userRoleMapper.insert(userRole);
		redisUtil.hset(RedisConsts.A_USERROLE, String.valueOf(uid), JSONObject.toJSONString(userRole));
	}
}
